dfs and similar graphs trees *1300

Please click on ads to support us..

Python Code:

n = int(input())

tree = {}
node_count_edges = {}
u, v = map(int, input().split())
tree[u] = [v]
tree[v] = [u]
root = u
for i in range(n - 2):
    u, v = map(int, input().split())
    if u not in tree: tree[u] = []
    if v not in tree: tree[v] = []
    tree[u].append(v)
    tree[v].append(u)

curr_nodes = [root]
l = 0
r = 0
i = 0
node_side = {}
passed = set()
while len(curr_nodes) != 0:
    new_curr_nodes = []
    for node in curr_nodes:
        if node in passed:
            continue
        passed.add(node)
        new_curr_nodes.extend(tree[node])
        if i % 2 == 0:
            node_side[node] = 'left'
        else:
            node_side[node] = 'right'

        if i % 2 == 0:
            l += 1
        else:
            r += 1

    i += 1
    curr_nodes = new_curr_nodes


ans = 0
for node in tree:
    if node_side[node] == 'left':
        ans += r - len(tree[node])
    else:
        ans += l - len(tree[node])

print(ans // 2)

C++ Code:

/*亲爱的上帝,我求你给我力量来赢得这场战斗。*/
#include<bits/stdc++.h> 
using namespace std;
using i64 = long long;
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define vl vector<i64>
#define vi vector<int>
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}

constexpr int mxN = 2e5;
vector<vector<int>> A(mxN);
vector<bool> vis(mxN), color(mxN, 0);
void solve() {
    int N;
    cin >> N;
    for(int i=0; i<N-1; i++) {
        int u, v;
        cin >> u >> v;
        A[--u].push_back(--v);
        A[v].push_back(u);
    }
    i64 ONE = 0;
    function<void(int, int)> dfs = [&](int x, int col) {
        vis[x] = 1;
        color[x] = col;
        if(color[x]) ONE++;
        for(int j:A[x]) {
            if(!vis[j]) {
                dfs(j, col ^ 1);
            } 
        }

    };
   
    for(int i=0; i<N; i++) {
        if(!vis[i]) {
            dfs(i, 1);
        }
    }
    cout << ONE *1LL* (N - ONE) - N + 1;
    
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int TT=1; 
    // cin >> TT;
    while(TT--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array